<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 * Copyright (C) 2005 Leo Feyer
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at http://www.gnu.org/licenses/.
 *
 * PHP version 5
 * @copyright  include7 AG, Flavian Sierk 
 * @author     Flavian Sierk 
 * @package    TellAFriend 
 * @license    LGPL 
 * @filesource
 */


/**
 * Class ModuleTellAFriend 
 *
 * @copyright  include7 AG, Flavian Sierk 
 * @author     Flavian Sierk 
 * @package    Controller
 */
class ModuleTellAFriend extends Module
{

	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'i7_tellafriend';
	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			$objTemplate = new BackendTemplate('be_wildcard');
			$objTemplate->wildcard = '### Tell-A-Friend ###';

			return $objTemplate->parse();
		}
		return parent::generate();
	}

	/**
	 * Generate module
	 */
	protected function compile()
	{
		$arrFields = array();

		if($this->tellafriend_mailthis){
			if($this->tellafriend_senderemail){
				$arrFields['sender_email'] = array
				(
					'name' => 'sender_email',
					'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_sender_email'],
					'inputType' => 'text',
					'eval' => array('rgxp'=>'email', 'mandatory'=>true, 'maxlength'=>128, 'decodeEntities'=>true)
				);
			}
			if($this->tellafriend_sendername){
				$arrFields['sender_name'] = array
				(
					'name' => 'sender_name',
					'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_sender_name'],
					'inputType' => 'text',
					'eval' => array('mandatory'=>true, 'maxlength'=>128, 'decodeEntities'=>true)
				);
			}
			$arrFields['receiver_email'] = array
			(
				'name' => 'receiver_email',
				'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_receiver_email'],
				'inputType' => 'text',
				'eval' => array('rgxp'=>'email', 'mandatory'=>true, 'maxlength'=>128, 'decodeEntities'=>true)
			);
			$arrFields['receiver_name'] = array
			(
				'name' => 'receiver_name',
				'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_receiver_name'],
				'inputType' => 'text',
				'eval' => array('mandatory'=>true, 'maxlength'=>128, 'decodeEntities'=>true)
			);
			if($this->tellafriend_allowcustomtext){
				$arrFields['text'] = array
				(
					'name' => 'text',
					'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_text'],
					'inputType' => 'textarea',
					'eval' => array('rows'=>4, 'cols'=>40,'rgxp'=>'alphanum', 'decodeEntities'=>true)
				);
			}
			if(!$this->tellafriend_disablecaptcha) {
				$arrFields['tellafriend_captcha'] = array
				(
					'name' => 'tellafriend_captcha',
					'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_captcha'],
					'inputType' => 'captcha',
					'eval' => array('mandatory'=>true)
				);
			}
			if($this->tellafriend_disclaimer){
				
				$objPage = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
										  ->limit(1)
										  ->execute($this->tellafriend_disclaimerpage);

				if ($objPage->numRows)
				{
					$strLink = $this->generateFrontendUrl($objPage->row());
				}
				$strApprove = str_replace('%s', $strLink, $GLOBALS['TL_LANG']['MSC']['tellafriend_disclaimer']);
				$arrFields['tellafriend_disclaimer'] = array
				(
					'name' => 'tellafriend_disclaimer',
					'label' => $GLOBALS['TL_LANG']['MSC']['tellafriend_disclaimer_text'],
					'options' => array(1 => $strApprove),
					'inputType' => 'checkbox',
					'eval' => array('mandatory'=>true)
				);
			}
		}
		
		$doNotSubmit = false;
		$arrWidgets = array();

		// Initialize widgets
		foreach ($arrFields as $arrField)
		{
			$strClass = $GLOBALS['TL_FFL'][$arrField['inputType']];

			// Continue if the class is not defined
			if (!$this->classFileExists($strClass))
			{
				continue;
			}

			$arrField['eval']['required'] = $arrField['eval']['mandatory'];
			$objWidget = new $strClass($this->prepareForWidget($arrField, $arrField['name'], $arrField['value']));

			// Validate widget
			if ($this->Input->post('FORM_SUBMIT') == 'tellafriend')
			{
				$objWidget->validate();

				if ($objWidget->hasErrors())
				{
					$doNotSubmit = true;
				}
			}

			$arrWidgets[] = $objWidget;
		}
		
		$objPage = $this->Database->prepare("SELECT id, language FROM tl_page WHERE id=?")
								  ->limit(1)
								  ->execute($this->id);

		$page = $objPage->row();
		$language = $page['language'];
		
		if($this->tellafriend_addaddthis){
		$objTemplateTmp = new FrontendTemplate($this->tellafriend_addthistemplate);
		$addthisLanguages = array('en', 'zh', 'da', 'nl', 'fi', 'fr', 'de', 'he', 'it', 'ja', 'ko', 'no', 'pl', 'pt', 'ru', 'es', 'sv');
		if(!in_array($language, $addthisLanguages)){
			$language = 'en';
		}
		$objTemplateTmp->language = $language;
		$objTemplateTmp->addthiadvanced = $this->tellafriend_addthiadvanced ;
		$objTemplateTmp->atui_cobrand = $this->tellafriend_atui_cobrand ;
		$objTemplateTmp->atservices_exclude = $this->tellafriend_atservices_exclude ;
		$objTemplateTmp->atservices_compact = $this->tellafriend_atservices_compact ;
		$objTemplateTmp->atservices_expanded = $this->tellafriend_atservices_expanded ;
		$objTemplateTmp->atui_508_compliant = $this->tellafriend_atui_508_compliant ;
		$objTemplateTmp->atdata_ga_tracker = $this->tellafriend_atdata_ga_tracker ;
		$objTemplateTmp->atdata_use_flash = $this->tellafriend_atdata_use_flash ;
		$objTemplateTmp->atdata_use_cookies = $this->tellafriend_atdata_use_cookies ;
		$objTemplateTmp->atfcolor = $this->tellafriend_atfcolor;
		$objTemplateTmp->atbcolor = $this->tellafriend_atbcolor;
		$objTemplateTmp->atclick = $this->tellafriend_atclick;
		$objTemplateTmp->tellafriend_addthisuser = $this->tellafriend_addthisuser;
		$objTemplateTmp->tellafriend_addaddthis = $this->tellafriend_addaddthis;
		$this->Template->addthis = $objTemplateTmp->parse();
		}
		
		$this->Template->fields = $arrWidgets;
		
		$this->Template->tellafriend_mailthis 	= $this->tellafriend_mailthis;
		$this->Template->submit 								= $GLOBALS['TL_LANG']['MSC']['tellafriend_submit'];
		$this->Template->action 								= ampersand($this->Environment->request);
		
		if ($this->Input->post('FORM_SUBMIT') == 'tellafriend' && !$doNotSubmit)
		{
			$arrSet = array
			(
				'text' 						=> $this->Input->post('text'),
				'receiver_email' 	=> $this->Input->post('receiver_email', true),
				'receiver_name' 	=> $this->Input->post('receiver_name', true),
				'sender_email' 		=> $this->Input->post('sender_email', true),
				'sender_name' 		=> $this->Input->post('sender_name', true)
			);
			
				
				
				$this->log('Tell A Friend of '.$this->Environment->url . $this->Environment->requestUri.' has been sent to '.$arrSet['receiver_email'], 'Send TellAFriendEmail', 'TellAFriend');  
				
				$objEmail = new Email();
				
				
				$objEmail->subject = $this->tellafriend_subject;

				$text = strip_tags($arrSet['text']);
				$strData = $this->tellafriend_mailtext;
				$strData = str_replace("##url##", $this->Environment->url . $this->Environment->requestUri, $strData);
				$strData = str_replace('##visitortext##',  $text, $strData);
				$strData = str_replace('##receiver-email##',  $arrSet['receiver_email'], $strData);
				$strData = str_replace('##receiver-name##',  $arrSet['receiver_name'], $strData);
				$strData = str_replace('##sender-email##',  $arrSet['sender_email'], $strData);
				$strData = str_replace('##sender-name##',  $arrSet['sender_name'], $strData);
				
				$objEmail->text = $strData;
				$objEmail->from = $GLOBALS['TL_ADMIN_EMAIL'];
				if($arrSet['sender_email']){
					$objEmail->replyTo($arrSet['sender_name']." <".$arrSet['sender_email'].">");
				}
				$objEmail->sendTo($arrSet['receiver_email']);
				
				
				// EMPTY FORM
				$this->Input->setPost('text', "");
				$this->Input->setPost('receiver_email', "");
				$this->Input->setPost('receiver_name', "");
				$this->Input->setPost('sender_email', "");
				$this->Input->setPost('sender_name', "");
				$this->Input->setPost('disclaimer', "");
				$arrWidgets = array();
				foreach ($arrFields as $arrField)
				{
					$strClass = $GLOBALS['TL_FFL'][$arrField['inputType']];
					// Continue if the class is not defined
					if (!$this->classFileExists($strClass))
					{
						continue;
					}
					$arrField['eval']['required'] = $arrField['eval']['mandatory'];
					$objWidget = new $strClass($this->prepareForWidget($arrField, $arrField['name'], $arrField['value']));
					$arrWidgets[] = $objWidget;
				}

				$this->Template->fields = $arrWidgets;
				
				$this->Template->message = $GLOBALS['TL_LANG']['MSC']['tellafriend_thanks'];
		}		
	}
}

?>